package game;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Random;
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;
import com.golden.gamedev.object.background.*;

/**
 * A simple tower defense game.
 * 
 * @author Alex Lee (hl69)
 */
public class TowerDefense extends GameObject
{
	/*
	 * The game is based on 80x80 tiles.
	 */
	public static final int TILE_SIZE = 80;
	public static final int ROWS = 12, COLS = 10;
	Random random;

	PlayField playfield;

	Background background;
	Tower tower;
	Tower tower2;

	PacmanEnemy pacman;

	int money;

	SpriteGroup TOWERS, TOWER_PROJECTILES, ENEMIES, ENEMY_PROJECTILES, TREASURES;
	int treasureCount;

	Timer levelTimer, spawnTimer;

	public TowerDefense(GameEngine parent)
	{
		super(parent);
	}

	/**
	 * Initializes all the resources used in the game, such as sprites, images,
	 * and sounds.
	 */
	public void initResources()
	{
		/*
		 * Instantiate some useful objects.
		 */
		random = new Random();

		/*
		 * Define the background image. Currently, it's a nice grassy field.
		 */
		background = new TileBackground(getImages("resources/grass.jpg", 1, 1), ROWS, COLS);
		playfield = new PlayField(background);

		/*
		 * Instantiate sprite groups.
		 */
		TOWERS = playfield.addGroup(new SpriteGroup("Towers"));
		TOWER_PROJECTILES = playfield.addGroup(new SpriteGroup("Tower Projectiles"));
		ENEMIES = playfield.addGroup(new SpriteGroup("Enemies"));
		ENEMY_PROJECTILES = playfield.addGroup(new SpriteGroup("Enemy Projectiles"));
		TREASURES = playfield.addGroup(new SpriteGroup("Treasures"));

		/*
		 * Start with some money.
		 */
		money = 100;

		/*
		 * Level goes up every minute.
		 */
		levelTimer = new Timer(60000);

		/*
		 * Initially, enemies are spawned every five seconds.
		 */
		spawnTimer = new Timer(5000);

		/*
		 * Set up a wall of treasure.
		 */
		for(int i = 0; i < COLS; i++)
		{
			TREASURES.add(new Sprite(getImage("resources/treasure.png"), 0, i*TILE_SIZE));
			treasureCount++;
		}

		/*
		 * Manage collisions.
		 */
		playfield.addCollisionGroup(TOWER_PROJECTILES, ENEMIES, new ProjectileEnemyCollision());
		playfield.addCollisionGroup(ENEMIES, TOWERS, new EnemyTowerCollision());
		playfield.addCollisionGroup(ENEMIES, TREASURES, new EnemyTreasureCollision());
	}

	/**
	 * Updates sprites upon user input.
	 */
	public void update(long elapsedTime)
	{
		playfield.update(elapsedTime);

		updateTowers(elapsedTime);

		updateEnemies(elapsedTime);

		/*
		 * Whenever the level goes up, make the enemies spawn a little faster.
		 */
		if(levelTimer.action(elapsedTime)) levelUp();

		/*
		 * Spawn enemies according to the spawn timer.
		 */
		if(spawnTimer.action(elapsedTime)) spawn();

		/*
		 * Quit the game if escape is pressed.
		 */
		if(keyDown(KeyEvent.VK_ESCAPE))
		{
			finish();
		}
	}
	
	private void levelUp()
	{
		/*
		 * Each time the player levels up, make enemies spawn 0.5 seconds
		 * faster.
		 */
		Timer newTimer = new Timer(100);
		newTimer.setEquals(spawnTimer);
		long delay = newTimer.getDelay();
		newTimer.setDelay(delay - 500);
	}

	private void spawn()
	{
		ENEMIES.add(new PacmanEnemy(this, TILE_SIZE*ROWS, randomY()));
	}

	/**
	 * Gets the x-coordinate of the top-left corner of a random tile.
	 * @return
	 */
	private int randomX()
	{
		return TILE_SIZE * random.nextInt(ROWS);
	}

	/**
	 * Gets the y-coordinate of the top-left corner of a random tile.
	 * @return
	 */
	private int randomY()
	{
		return TILE_SIZE * random.nextInt(COLS);
	}

	private void updateTowers(long elapsedTime)
	{

	}

	private void updateEnemies(long elapsedTime)
	{

	}

	/**
	 * Renders graphics.
	 */
	public void render(Graphics2D g)
	{
		playfield.render(g);

		fontManager.getFont("FPS Font").drawString(g, "MONEY = "
				+ money, 10, 10);
	}

}
